package com.gauravk.audiovisualizersample.slice;

import com.gauravk.audiovisualizersample.ResourceTable;
import com.gauravk.audiovisualizersample.ui.*;
import com.gauravk.audiovisualizersample.util.PermissionUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.bundle.IBundleManager;
import ohos.global.resource.RawFileDescriptor;
import ohos.hiviewdfx.HiLog;
import ohos.media.common.Source;
import ohos.media.player.Player;

import java.io.IOException;
import java.util.logging.Logger;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        super.setUIContent(ResourceTable.Layout_ability_main);
        initReqPermissions();
        initView();
    }

    private void initView() {
        findComponentById(ResourceTable.Id_bt_main_blob).setClickedListener(this);
        findComponentById(ResourceTable.Id_bt_main_blast).setClickedListener(this);
        findComponentById(ResourceTable.Id_bt_main_wave).setClickedListener(this);
        findComponentById(ResourceTable.Id_bt_main_bar).setClickedListener(this);
        findComponentById(ResourceTable.Id_bt_main_stream).setClickedListener(this);
        findComponentById(ResourceTable.Id_bt_main_circle_line).setClickedListener(this);
        findComponentById(ResourceTable.Id_bt_main_hifi).setClickedListener(this);
    }

    @Override
    protected void onActive() {
        super.onActive();
        try {
            RawFileDescriptor filDescriptor = getResourceManager().getRawFileEntry("resources/rawfile/sample.aac").openRawFileDescriptor();
            Source source = new Source(filDescriptor.getFileDescriptor());
            Player impl = new Player(this);
            impl.setSource(source);
            impl.prepare();
            impl.play();
        } catch (IOException e) {
            Logger.getGlobal().finest(e.getMessage());
        }
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_bt_main_blob:
                nextPage(BlobAbility.class.getName());
                break;
            case ResourceTable.Id_bt_main_blast:
                nextPage(BlastAbility.class.getName());
                break;
            case ResourceTable.Id_bt_main_wave:
                nextPage(WaveAbility.class.getName());
                break;
            case ResourceTable.Id_bt_main_bar:
                nextPage(BarAbility.class.getName());
                break;
            case ResourceTable.Id_bt_main_stream:
                nextPage(MusicStreamAbility.class.getName());
                break;
            case ResourceTable.Id_bt_main_circle_line:
                nextPage(CircleLineAbility.class.getName());
                break;
            case ResourceTable.Id_bt_main_hifi:
                nextPage(HiFiAbility.class.getName());
                break;
            default:
                break;
        }
    }

    public void nextPage(String abilityName) {
        Intent intent = new Intent();
        Operation build = new Intent.OperationBuilder()
                .withBundleName(getBundleName())
                .withAbilityName(abilityName)
                .build();
        intent.setOperation(build);
        startAbility(intent);
    }

    private void initReqPermissions() {
        if (verifySelfPermission(PermissionUtils.MICROPHONE) != IBundleManager.PERMISSION_GRANTED
                || verifySelfPermission(PermissionUtils.READ_MEDIA) != IBundleManager.PERMISSION_GRANTED
                || verifySelfPermission(PermissionUtils.WRITE_MEDIA) != IBundleManager.PERMISSION_GRANTED
                || verifySelfPermission(PermissionUtils.RECORD_AUDIO) != IBundleManager.PERMISSION_GRANTED
                || verifySelfPermission(PermissionUtils.INTERNET) != IBundleManager.PERMISSION_GRANTED
        ) {
            if (canRequestPermission(PermissionUtils.MICROPHONE)
                    || canRequestPermission(PermissionUtils.READ_MEDIA)
                    || canRequestPermission(PermissionUtils.WRITE_MEDIA)
                    || canRequestPermission(PermissionUtils.RECORD_AUDIO)) {
                requestPermissionsFromUser(
                        PermissionUtils.permission, PermissionUtils.PERMISSION_CODE);
            }
        }
    }

}
